﻿using System;
using System.Collections.Generic;
using System.Reflection;
using System.Windows;
using GalaSoft.MvvmLight;
using GalaSoft.MvvmLight.Messaging;
using WP7Contrib.Common;
using WP7Contrib.Services.Navigation;
using WP7Contrib.Services.Settings;
using WP7Contrib.Services.Storage;

namespace DepSoft.Mobile.WP7.FollwIt.ViewModel
{
    public abstract class ViewModelBaseWp7 : ViewModelBase
    {
        #region Private members
        private readonly INavigationService _navigationService;
        private readonly IStorageService _storageService;
        private readonly IStoreSettings _settingsService;
        #endregion

        #region Public properties
        public INavigationService NavigationService
        {
            get
            {
                return this._navigationService;
            }
        }

        public IStoreSettings SettingsService
        {
            get
            {
                return this._settingsService;
            }
        }
        #endregion

        #region Constructor
        /// <summary>
        /// Initializes a new instance of the <see cref="ViewModelBaseWp7"/> class.
        /// </summary>
        protected ViewModelBaseWp7()
        {
        }

        /// <summary>
        /// </summary>
        /// <param name="storageService"></param>
        /// <param name="settingsService"></param>
        /// <param name="messenger"></param>
        protected ViewModelBaseWp7(INavigationService navigationService, IStorageService storageService, IStoreSettings settingsService, IMessenger messenger)
            : base(messenger)
        {
            this._navigationService = navigationService;
            this._storageService = storageService;
            this._settingsService = settingsService;

            var assemblies = new List<Assembly>
                                 {
                                     this.GetType().Assembly,
                                     typeof(BaseModel).Assembly
                                 };
        }

        /// <summary>
        /// Dispose pattern
        /// </summary>
        ~ViewModelBaseWp7()
        {
            this.Dispose();
        }
        #endregion

        #region Private methods
        protected abstract void IsBeingActivated(IStorage storage);

        protected abstract void IsBeingDeactivated(IStorage storage);

        /// <summary>
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <param name="currentValue"></param>
        /// <param name="newValue"></param>
        /// <param name="propertyName"></param>
        protected void SetPropertyAndNotify<T>(ref T currentValue, T newValue, string propertyName)
        {
            if (Equals(currentValue, newValue))
            {
                return;
            }

            currentValue = newValue;
            this.RaisePropertyChanged(propertyName);
        }

        /// <summary>
        /// </summary>
        /// <param name="uiAction"></param>
        protected void ExecuteOnUIThread(Action uiAction)
        {
            if (Deployment.Current.Dispatcher.CheckAccess())
            {
                uiAction();
            }
            else
            {
                Deployment.Current.Dispatcher.BeginInvoke(uiAction);
            }
        }
        #endregion

        #region Public methods
        public void Activate()
        {
            using (var storage = this._storageService.OpenTransient(this.GetType().Name))
            {
                this.IsBeingActivated(storage);
            }
        }

        public void Deactivate()
        {
            using (var storage = this._storageService.OpenTransient(this.GetType().Name))
            {
                this.IsBeingDeactivated(storage);
            }
        }
        #endregion
    }
}
